package com.sxw.page;

import com.github.pagehelper.PageHelper;
import com.sxw.utils.Convert;
import com.sxw.utils.SqlUtil;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.util.MultiValueMap;

import java.util.Map;

/**
 * 分页工具类
 * 
 * @author ruoyi
 */
public class PageUtils extends PageHelper
{

    /**
     * 当前记录起始索引
     */
    public static final String PAGE_NUM = "pageNum";

    /**
     * 每页显示记录数
     */
    public static final String PAGE_SIZE = "pageSize";

    /**
     * 排序列
     */
    public static final String ORDER_BY_COLUMN = "orderByColumn";

    /**
     * 排序的方向 "desc" 或者 "asc".
     */
    public static final String IS_ASC = "isAsc";

    /**
     * 分页参数合理化
     */
    public static final String REASONABLE = "reasonable";

    /**
     * 设置请求分页数据
     */
    public static void startPage(ServerHttpRequest serverHttpRequest)
    {
        MultiValueMap<String, String> mm =  serverHttpRequest.getQueryParams();
        Integer pageNum = Convert.toInt(mm.getFirst(PAGE_NUM), 1);
        Integer pageSize = Convert.toInt(mm.getFirst(PAGE_SIZE), 10);
        String orderBy = SqlUtil.escapeOrderBySql(mm.getFirst(ORDER_BY_COLUMN));
        Boolean reasonable = Boolean.valueOf(mm.getFirst(REASONABLE));
        PageHelper.startPage(pageNum, pageSize, orderBy).setReasonable(reasonable);
    }

    /**
     * 清理分页的线程变量
     */
    public static void clearPage()
    {
        PageHelper.clearPage();
    }
}
